﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Tsundere
{
    class Objeto3D
    {
        Model modelo;

        public Vector3 posicao;
        public Vector3 rotacao;
        public Vector3 escala;

        Texture2D textura;



        public Objeto3D(Model modelo)
        {
            this.modelo = modelo;

            posicao = new Vector3(0, 10, 0);
            rotacao = new Vector3(0, 0, 0);
            escala = new Vector3(1, 1, 1);

            textura = Game1.Self.Content.Load<Texture2D>("Alien Probe/textures/Probe_col");
        }

        public void Update(GameTime gameTime)
        {
            //this.rotacao.Y += 1;

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                posicao.Z += 0.3f;
            }

        }

        public Matrix worldMatrx
        {
            get
            {
                Matrix matriz = Matrix.Identity
                    * Matrix.CreateScale(escala)
                    * Matrix.CreateRotationX(MathHelper.ToRadians(rotacao.X))
                    * Matrix.CreateRotationY(MathHelper.ToRadians(rotacao.Y))
                    * Matrix.CreateRotationZ(MathHelper.ToRadians(rotacao.Z))
                    * Matrix.CreateTranslation(posicao);



                return matriz;
            }
        }




        public void Draw(Camera3D camera, GraphicsDevice device)
        {
            //modelo.Draw(worldMatrx, camera.View, camera.projecao);

            foreach (ModelMesh malha in modelo.Meshes)
            {
                foreach (ModelMeshPart subMalha in malha.MeshParts)
                {
                    BasicEffect basicEffect = new BasicEffect(device);
                    basicEffect.World = worldMatrx;
                    basicEffect.View = camera.View;
                    basicEffect.Projection = camera.projecao;
                    basicEffect.Texture = textura;
                    basicEffect.TextureEnabled = true;
                    //basicEffect.EnableDefaultLighting();



                    basicEffect.LightingEnabled = true;
                    basicEffect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0);
                    basicEffect.DirectionalLight0.Direction = new Vector3(1, 0, 0);
                    basicEffect.DirectionalLight0.SpecularColor = new Vector3(0, 0, 1);

                    basicEffect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f);
                    basicEffect.EmissiveColor = Color.Red.ToVector3();

                    //basicEffect.DirectionalLight1.Enabled = true;
                    //basicEffect.DirectionalLight1.Direction = new Vector3(22, 11, 56);
                    //basicEffect.DirectionalLight1.SpecularColor = Color.Black.ToVector3();
                    //basicEffect.DirectionalLight1.DiffuseColor = Color.Yellow.ToVector3();

                    //basicEffect.DirectionalLight2.Enabled = true;
                    //basicEffect.DirectionalLight2.Direction = new Vector3(-22, -11, -56);
                    //basicEffect.DirectionalLight2.SpecularColor = Color.Green.ToVector3();
                    //basicEffect.DirectionalLight2.DiffuseColor = Color.Purple.ToVector3();

                    //basicEffect.AmbientLightColor = Color.Green.ToVector3();

                    basicEffect.FogEnabled = true;
                    basicEffect.FogColor = Color.Black.ToVector3(); 
                    basicEffect.FogStart = 80;
                    basicEffect.FogEnd = 100;

                    //basicEffect.EmissiveColor = Color.Purple.ToVector3();
                                        

                    basicEffect.LightingEnabled = true;
                    //basicEffect.Alpha = 0.5f;

                    subMalha.Effect = basicEffect;
                }
                malha.Draw();
            }

        }
    }
}
